package sv.com.renacempleo.web.controller.ciiu;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import sv.com.renacempleo.dto.ciiu.ActividadesRegistroContribuyente;
import sv.com.renacempleo.dto.ciiu.CIIU_01;
import sv.com.renacempleo.dto.ciiu.CIIU_02;
import sv.com.renacempleo.dto.ciiu.CIIU_03;
import sv.com.renacempleo.dto.ciiu.CIIU_04;
import sv.com.renacempleo.exception.WebException;
import sv.com.renacempleo.web.controller.BaseController;
import sv.com.renacempleo.web.util.DBContext;
import sv.com.renacempleo.web.util.WebUtilidades;

@SuppressWarnings("serial")
@Controller
@RequestMapping("/ciiu")
public class CiiuController extends BaseController{

	@RequestMapping(value = "/view", method = {RequestMethod.GET, RequestMethod.POST})
	public String view(ModelMap model, 
			@RequestParam (required = false) String header) {
		model.addAttribute(WebUtilidades.HEADER_TITLE, StringUtils.isBlank(header) ? "CIIU" : header);
		
		return "ciiu/redCiiu";
	}
	
	@RequestMapping(value = "/buscarCiiu01", method = {RequestMethod.GET, RequestMethod.POST})
	public void buscarCiiu01(HttpServletResponse response){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("ciiu01", JSONArray.fromObject(
					DBContext.getCiiu_01Dao().findAllAsArray()));
		}catch (Exception e) {
			logger.error(e);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/guardarCiiu01", method = {RequestMethod.GET, RequestMethod.POST})
	public void guardarCiiu01(HttpServletResponse response, 
			@ModelAttribute("ciiu01") CIIU_01 ciiu01){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCiiu_01Dao().save(ciiu01);
			result.put("ciiu01", JSONArray.fromObject(
					DBContext.getCiiu_01Dao().findAllAsArray()));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		result.put(MESSAGE, result.get(MESSAGE));
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/eliminarCiiu01", method = {RequestMethod.GET, RequestMethod.POST})
	public void eliminarCiiu01(HttpServletResponse response,
			@RequestParam String ciiu01Codigo){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCiiu_01Dao().delete(DBContext.getCiiu_01Dao().findByIdStr(CIIU_01.class, ciiu01Codigo));
			result.put("ciiu01", JSONArray.fromObject(
					DBContext.getCiiu_01Dao().findAllAsArray()));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		result.put(MESSAGE, result.get(MESSAGE));
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/buscarCiiu02ByCiiu01", method = {RequestMethod.GET, RequestMethod.POST})
	public void buscarCiiu02ByCiiu01(HttpServletResponse response,
			@RequestParam String ciiu01Codigo){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("ciiu02", JSONArray.fromObject(
					DBContext.getCiiu_02Dao().findAllByCIIU01(ciiu01Codigo)));
		}catch (Exception e) {
			logger.error(e);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/guardarCiiu02", method = {RequestMethod.GET, RequestMethod.POST})
	public void guardarCiiu02(HttpServletResponse response,
			@ModelAttribute("ciiu02") CIIU_02 ciiu02){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCiiu_02Dao().save(ciiu02);
			result.put("ciiu02", JSONArray.fromObject(
					DBContext.getCiiu_02Dao().findAllByCIIU01(ciiu02.getCiiu01().getCiiu01Codigo())));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/eliminarCiiu02", method = {RequestMethod.GET, RequestMethod.POST})
	public void eliminarCiiu02(HttpServletResponse response,
			@RequestParam String ciiu01Codigo, @RequestParam String ciiu02Codigo){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCiiu_02Dao().delete(ciiu01Codigo, ciiu02Codigo);
			result.put("ciiu02", JSONArray.fromObject(
					DBContext.getCiiu_02Dao().findAllByCIIU01(ciiu01Codigo)));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		result.put(MESSAGE, result.get(MESSAGE));
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/buscarCiiu03ByCiiu01AndCiiu02", method = {RequestMethod.GET, RequestMethod.POST})
	public void buscarCiiu03ByCiiu01AndCiiu02(HttpServletResponse response,
			@RequestParam String ciiu01Codigo, @RequestParam String ciiu02Codigo){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("ciiu03", JSONArray.fromObject(
					DBContext.getCiiu_03Dao().findAllByCIIU02(ciiu01Codigo,ciiu02Codigo)));
		}catch (Exception e) {
			logger.error(e);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/guardarCiiu03", method = {RequestMethod.GET, RequestMethod.POST})
	public void guardarCiiu03(HttpServletResponse response,
			@ModelAttribute("ciiu03") CIIU_03 ciiu03){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCiiu_03Dao().save(ciiu03);
			result.put("ciiu03", JSONArray.fromObject(
					DBContext.getCiiu_03Dao().findAllByCIIU02(ciiu03.getCiiu01().getCiiu01Codigo(),
							ciiu03.getCiiu02().getCiiu02Codigo())));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/eliminarCiiu03", method = {RequestMethod.GET, RequestMethod.POST})
	public void eliminarCiiu03(HttpServletResponse response,
			@RequestParam String ciiu01Codigo, @RequestParam String ciiu02Codigo
			, @RequestParam String ciiu03Codigo){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCiiu_03Dao().delete(ciiu01Codigo, ciiu02Codigo, ciiu03Codigo);
			result.put("ciiu03", JSONArray.fromObject(
					DBContext.getCiiu_03Dao().findAllByCIIU02(ciiu01Codigo,ciiu02Codigo)));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		result.put(MESSAGE, result.get(MESSAGE));
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/buscarCiiu03ByCiiu01AndCiiu02AndCiiu03", method = {RequestMethod.GET, RequestMethod.POST})
	public void buscarCiiu03ByCiiu01AndCiiu02AndCiiu03(HttpServletResponse response,
			@RequestParam String ciiu01Codigo, @RequestParam String ciiu02Codigo, 
			@RequestParam String ciiu03Codigo){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("ciiu04", JSONArray.fromObject(
					DBContext.getCiiu_04Dao().findAllByCIIU03(ciiu01Codigo,ciiu02Codigo,ciiu03Codigo)));
		}catch (Exception e) {
			logger.error(e);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/guardarCiiu04", method = {RequestMethod.GET, RequestMethod.POST})
	public void guardarCiiu04(HttpServletResponse response,
			@ModelAttribute("ciiu04") CIIU_04 ciiu04){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCiiu_04Dao().save(ciiu04);
			result.put("ciiu04", JSONArray.fromObject(
					DBContext.getCiiu_04Dao().findAllByCIIU03(ciiu04.getCiiu01().getCiiu01Codigo(),
							ciiu04.getCiiu02().getCiiu02Codigo(),ciiu04.getCiiu03().getCiiu03Codigo())));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/eliminarCiiu04", method = {RequestMethod.GET, RequestMethod.POST})
	public void eliminarCiiu04(HttpServletResponse response,
			@RequestParam String ciiu01Codigo, @RequestParam String ciiu02Codigo
			, @RequestParam String ciiu03Codigo, @RequestParam String ciiu04Codigo){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getCiiu_04Dao().delete(ciiu01Codigo, ciiu02Codigo, ciiu03Codigo, ciiu04Codigo);
			result.put("ciiu04", JSONArray.fromObject(
					DBContext.getCiiu_04Dao().findAllByCIIU03(ciiu01Codigo,
							ciiu02Codigo,ciiu03Codigo)));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		result.put(MESSAGE, result.get(MESSAGE));
		serializeObject(result, response);
	}
	
	
	@RequestMapping(value = "/buscarActividadByCiiu01To04", method = {RequestMethod.GET, RequestMethod.POST})
	public void buscarActividadByCiiu01To04(HttpServletResponse response,
			@RequestParam String ciiu01Codigo, @RequestParam String ciiu02Codigo, 
			@RequestParam String ciiu03Codigo,@RequestParam String ciiu04Codigo){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			result.put("actividad", JSONArray.fromObject(
					DBContext.getActividadesRegistroContribuyenteDao().findAllByCIIU04(ciiu01Codigo,ciiu02Codigo,ciiu03Codigo,ciiu04Codigo)));
		}catch (Exception e) {
			logger.error(e);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/guardarActividad", method = {RequestMethod.GET, RequestMethod.POST})
	public void guardarActividad(HttpServletResponse response,
			@ModelAttribute("actividadesRegistroContribuyente") ActividadesRegistroContribuyente actividadesRegistroContribuyente){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getActividadesRegistroContribuyenteDao().save(actividadesRegistroContribuyente);
			result.put("actividad", JSONArray.fromObject(
					DBContext.getActividadesRegistroContribuyenteDao().findAllByCIIU04(
							actividadesRegistroContribuyente.getCiiu01().getCiiu01Codigo(),
							actividadesRegistroContribuyente.getCiiu02().getCiiu02Codigo(),
							actividadesRegistroContribuyente.getCiiu03().getCiiu03Codigo(),
							actividadesRegistroContribuyente.getCiiu04().getCiiu04Codigo())));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		serializeObject(result, response);
	}
	
	@RequestMapping(value = "/eliminarActividad", method = {RequestMethod.GET, RequestMethod.POST})
	public void eliminarActividad(HttpServletResponse response,
			@RequestParam String ciiu01Codigo, @RequestParam String ciiu02Codigo,
			@RequestParam String ciiu03Codigo, @RequestParam String ciiu04Codigo,
			@RequestParam Long actividadId){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			DBContext.getActividadesRegistroContribuyenteDao().delete(ciiu01Codigo, ciiu02Codigo, ciiu03Codigo, ciiu04Codigo,actividadId);
			result.put("actividad", JSONArray.fromObject(
					DBContext.getActividadesRegistroContribuyenteDao().findAllByCIIU04(
							ciiu01Codigo,
							ciiu02Codigo,
							ciiu03Codigo,
							ciiu04Codigo)));
			result.put(MESSAGE, "Datos guardados exitosamente");
			result.put(SUCCESS, true);
		} catch (WebException e) {
			logger.error(e);
			result.put(MESSAGE, WebUtilidades.getExceptionMessage(e));
			result.put(FAILURE, true);
		}catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			result.put(MESSAGE, "Procesamiento Incorrecto");
			result.put(FAILURE, true);
		}
		result.put(MESSAGE, result.get(MESSAGE));
		serializeObject(result, response);
	}
}
